package com.gxa.insurance.controller;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.pojo.Admin;
import com.gxa.insurance.pojo.InsureApply;
import com.gxa.insurance.pojo.ThirdAdmin;
import com.gxa.insurance.service.ThirdAdminService;
import com.gxa.insurance.util.JsonResult;
import com.gxa.insurance.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 10076
 */
@Api(value = "第三方保险公司管理接口")
@RestController
@RequestMapping("/Third")
@CrossOrigin
public class ThirdController {
    @Autowired
    private ThirdAdminService thirdAdminService;



    @ApiOperation(value = "添加公司",produces = "query")
    @PostMapping("/addInsurance")
    public Map add(@RequestBody ThirdAdmin thirdAdmin) throws Exception {

        Integer insert = thirdAdminService.insert(thirdAdmin);

        System.out.println("插入的当前数据的id:"+thirdAdmin.getId());
        JsonResult jsonResult=new JsonResult();
        jsonResult.setMsg("添加数据成功");
        jsonResult.setCode("200");
        jsonResult.setData(insert);
        return jsonResult.getMap();
    }
    @ApiOperation(value = "通过id查询公司所有信息",produces = "query")
    @PostMapping("/getInsurance")
  public Map select(@RequestParam(value = "公司id") Integer id) throws Exception {
        ThirdAdmin thirdAdmin = thirdAdminService.selectById(id);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(thirdAdmin);
        return jsonResult.getMap();
    }
    @ApiOperation(value = "投保申请",produces = "query")
    @PostMapping("/applyInsure")
    public Map insert(@RequestBody InsureApply insureApply) throws Exception {
        Integer insert = thirdAdminService.insertApply(insureApply);
        // List<ThirdAdmin> thirdAdmins = thirdAdminService.selectAll();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加数据成功");
        jsonResult.setData("添加了"+insert+"条数据");
        return jsonResult.getMap();
    }

    @ApiOperation(value = "查询所有投保信息",produces = "query")
    @PostMapping("/selectByApply")
    public Map selectByApply(){
        List<InsureApply> insureApplies = thirdAdminService.selectAllApply();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(insureApplies);
        return jsonResult.getMap();
    }

    @ApiOperation(value = "查询所有公司所有信息",produces = "query")
    @PostMapping("/selectAll")
    public Map selectAll() throws Exception {
        List<ThirdAdmin> thirdAdmins = thirdAdminService.selectAll();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(thirdAdmins);
        return jsonResult.getMap();
    }

    @ApiOperation(value = "查询所有公司名字",produces = "query")
    @PostMapping("/selectCname")
    public Map selectName() throws Exception {
        List<String> list = thirdAdminService.selectAllByname();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(list);
        return jsonResult.getMap();
    }


    @ApiOperation(value = "自定义添加公司",produces = "query")
    @PostMapping("/defined")
    public Map addByDefined(@RequestParam("CName")String companyName) throws BusinessException {
        if(StringUtil.isEmpty(companyName)){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        JsonResult jsonResult = new JsonResult();
        ThirdAdmin thirdAdmin = new ThirdAdmin();
        // 创建了对象就给默认值
        thirdAdmin.setName(companyName);
        Integer insert = thirdAdminService.insert(thirdAdmin);
        if(insert<=0){
            jsonResult.setCode("0");
            jsonResult.setMsg("自定义失败");
        }else{
            jsonResult.setCode("1");
            jsonResult.setMsg("自定义成功");
        }
        return jsonResult.getMap();
    }

    @ApiOperation(value = "通过id删除公司",produces = "query")
    @PostMapping("/delect")
    public Map delectById(Integer id) throws Exception {
       thirdAdminService.delect(id);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getMap();
    }

    @ApiOperation(value = "修改公司",produces = "query")
    @PostMapping("/update")
    public Map updateById(@RequestBody ThirdAdmin thirdAdmin) throws Exception {

        thirdAdminService.update(thirdAdmin);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }
}
